package com.zkx.laolaiyi.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.zkx.laolaiyi.constant.ResultCodeConstant;
import com.zkx.laolaiyi.constant.ResultMsgConstant;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 返回结果
 * @param <T>
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> {
    private String code;
    private String msg;
    private T data;
    private String token;

    /**
     * 有数据的成功返回
     * @param data 响应数据
     * @return
     * @param <T> 响应数据类型
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.code = ResultCodeConstant.SUCCESS;
        result.msg = ResultMsgConstant.SUCCESS;
        result.data = data;
        return result;
    }

    /**
     * 无数据的成功返回
     * @return
     * @param <T> 响应数据类型
     */
    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.code = ResultCodeConstant.SUCCESS;
        result.msg = ResultMsgConstant.SUCCESS;
        return result;
    }

    /**
     * 有数据、tokne的成功返回
     * @param data
     * @param token
     * @return
     * @param <T>
     */
    public static <T> Result<T> success(T data, String token) {
        Result<T> result = new Result<>();
        result.code = ResultCodeConstant.SUCCESS;
        result.msg = ResultMsgConstant.SUCCESS;
        result.data = data;
        result.token = token;
        return result;
    }

    /**
     * 有token的成功返回
     */
    public static <T> Result<T> success(String token) {
        Result<T> result = new Result<>();
        result.code = ResultCodeConstant.SUCCESS;
        result.msg = ResultMsgConstant.SUCCESS;
        result.token = token;
        return result;
    }

    /**
     * 返回错误信息
     * @param code 状态码
     * @param errorMsg 错误信息
     * @return
     * @param <T>
     */
    public static <T> Result<T> error(String code, String errorMsg) {
        Result<T> result = new Result<>();
        result.code = code;
        result.msg = errorMsg;
        return result;
    }
}
